#pragma once

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

const std::string splitting_symbol = "\r\n";
const std::string root_directory = "./wwwroot";
const std::string home_page = "index.html";

class Request
{
public:
    void Deserialize(std::string &package)
    {
        while (true)
        {
            size_t pos = package.find(splitting_symbol);
            if (pos == std::string::npos)
                break; // 直接退出循环即可
            // return;
            std::string res = package.substr(0, pos);
            if (res.empty())
                break;
            req_head_.push_back(res);
            package.erase(0, pos + splitting_symbol.size());
        }
        text_ = package;
    }

    void Parse()
    {
        std::stringstream ss(req_head_[0]);
        ss >> method_ >> url_ >> version_;
        path_file_ = root_directory;
        if (url_ == "/" || url_ == "/index.html")
        {
            path_file_ += "/";
            path_file_ += home_page;
        }
        else
            path_file_ += url_;

        size_t pos = path_file_.rfind('.');
        if (pos == std::string::npos)
            suffix_ = ".html";
        else
            suffix_ = path_file_.substr(pos);
    }

    void Print()
    {
        for (auto &str : req_head_)
        {
            std::cout << "*******************" << std::endl;
            std::cout << str << std::endl;
        }
        std::cout << "method : " << method_ << std::endl;
        std::cout << "url : " << url_ << std::endl;
        std::cout << "http version : " << version_ << std::endl;
        std::cout << "path_file_ : " << path_file_ << std::endl;
        std::cout << "suffix_ : " << suffix_ << std::endl;
        std::cout << text_ << std::endl;
        std::cout << std::endl;
    }

    std::string GetPathFile()
    {
        return path_file_;
    }

    std::string GetSuffix()
    {
        return suffix_;
    }

private:
    std::vector<std::string> req_head_;
    std::string text_;
    std::string method_;
    std::string url_;
    std::string version_;
    std::string path_file_;
    std::string suffix_;
};